//db.js

/**
 * Created by 董鹏飞 on 2016/7/19.
 */
// 连接MySQL
var mysql = require('mysql');
var pool = mysql.createPool({
    host: process.env.DB_HOST || "localhost",
    user: process.env.DB_USERNAME || "root",
    password: process.env.DB_PASSWORD || "root",
    database: process.env.DB_DATABASE || "allinone",
    multipleStatements: true
});

function query(sql, callback) {
    pool.getConnection(function (err, connection) {
        // Use the connection
        connection.query(sql, function (err, rows) {
            if (err) {
                console.error("连接错误")
                console.error(err)
            } else {
                callback(err, rows);
                connection.release();//释放链接
            }
        });
    });
}


exports.query = query;
